home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / cprint.arc / CPRINT.C next >
Encoding:
C/C++ Source or Header  |  1986-01-15  |  7.4 KB  |  320 lines

  1. /*
  2. ┌────────────────────────────────────────────────────────────────────────────┐
  3. │ Title   : Cprint.c                                 │
  4. │ Purpose : Allow multiple printing of programs, with wildcards and various  │
  5. │                                         │
  6. │        formatting options.                          │
  7. │  -d specifies double strike printing                         │
  8. │  -e specifies emphasized printing                         │
  9. │  -t specifies title of document + time and date                 │
  10. │  -w specifies wide (compressed) listing                     │
  11. │  -n specifies to number the lines                         │
  12. │  -c specifies output to console                         │
  13. │  -mn specifies multiple copies where n is 1..num of copies             │
  14. │                                         │
  15. │Written using Lattice C v 2.15e, translated to Microsoft C v3.0  on 1/15/86 │
  16. │                                         │
  17. │    Written by Jack Zucker - 75766,1336    301-794-5950             │                                     │
  18. └────────────────────────────────────────────────────────────────────────────┘
  19. */
  20. #include <stdio.h>           /* standard header file      */
  21. #include <dos.h>           /* register definitions      */
  22. #include <time.h>           /* Ms C time struct      */
  23. #include <stdlib.h>           /* standard functions      */
  24. #include "jzdirect.h"          /* defines my dir struct   */
  25. #include "jzfndfst.c"          /* get first matching file */
  26. #include "jzfndnxt.c"          /* get next matching file  */
  27.  
  28. #define FF 0x0c           /* form feed          */
  29. #define PGLEN 60          /* length of a page      */
  30. #define PROUT 0x17          /* bios printer function      */
  31. #define COMPRS 0x0f          /* epson compressed code      */
  32. #define BUFSIZE 1024          /* text buffer size      */
  33. #define HEADING Listing of
  34. #define ESC 0x1b          /* hex esc code          */
  35. #define TITLE1 "Listing of "
  36. #define TITLE2 " on "
  37. #define TITLE3 "    PAGE: "
  38. #define DBL 71              /* epson double strike code */
  39. #define EMPH 69           /* epson emphasized code      */
  40.  
  41.  
  42. int lineno,colno,lineslft,width = 80,pagenum;
  43.  
  44. struct tm *wtime;
  45. char *wasctime;
  46.  
  47. TDIR wdir;
  48.  
  49. /* switches */
  50. int NUMBER = 0,WIDE = 0,TITLE = 0,CONSOLE = 0;
  51. int DOUBLE = 0,EMPHASIZ = 0,MULTIPLE = 1;
  52.  
  53. char mask[35];                    /* global variables */
  54. char wpath[35],wtemp[50];
  55. union REGS inregs,outregs;            /* register struct    */
  56.  
  57.  
  58. main (argc,argv)
  59. int argc;
  60. char *argv[];
  61.  
  62. {
  63.   char buf[BUFSIZE],*s,*ch;
  64.   long wtics;
  65.   int c,n,w;
  66.   FILE *fd;
  67.   int werr;
  68.  
  69.  
  70.   n = BUFSIZE;                       /* read buf size   */
  71.   strcpy(mask,*++argv);                /* mask = filespec */
  72.  
  73.   for (w = strlen(mask) ; w >= 0 ; w -- )
  74.     if (mask[w] == '\\' || mask[w] == ':') break;
  75.  
  76.   if (w >= 0) {
  77.     strcpy(wpath,mask);
  78.     wpath[++w] = '\0';
  79.   }
  80.   else *wpath = '\0';
  81.  
  82.   time(&wtics);
  83.   wtime = gmtime(&wtics);
  84.   wasctime = asctime(wtime);
  85.   w = strlen(wasctime);
  86.   wasctime[--w] = '\0';
  87.   while (--argc > 0 && (*++argv)[0] == '-')    /* parse through options */
  88.     for (s = argv[0]+1; *s ; s ++)
  89.       switch(*s) {
  90.     case 'w' :        /* wide printing */
  91.       WIDE = 1;
  92.       break;
  93.     case 'n' :        /* number lines  */
  94.       NUMBER = 1;
  95.       break;
  96.     case 't' :
  97.       TITLE = 1;     /* title and heading */
  98.       break;
  99.     case 'd' :
  100.       DOUBLE = 1;    /* double strike     */
  101.       break;
  102.     case 'e' :
  103.       EMPHASIZ = 1; /* emphasized         */
  104.       break;
  105.     case 'm' :
  106.       MULTIPLE = (*(++s) - 48); /* multiple copies */
  107.       break;
  108.     case 'c' :
  109.       CONSOLE = 1;    /* output to console (stdout) */
  110.       break;
  111.     default:    /* invalid option */
  112.       printf("\nIllegal option %c",*s);
  113.       argc = 0;
  114.       break;
  115.       }
  116.  
  117.                           /* help or bad options */
  118.   if (argc != 1 || (mask[0] == '?' && strlen(mask) == 1)) {
  119.     printf("\n\nCprint v2.0 by Jaz (Jack A. Zucker) 75766,1336");
  120.     printf("\nRevised for Microsoft C v3.0 1/14/86");
  121.     printf("\nusage CPRINT filespec (including wildcards) [-nwdetlimc]\n");
  122.     printf("\n\t-d specifies double strike printing");
  123.     printf("\n\t-e specifies emphasized printing");
  124.     printf("\n\t-t specifies title of document + time and date");
  125.     printf("\n\t-w specifies wide (compressed) listing");
  126.     printf("\n\t-n specifies to number the lines");
  127.     printf("\n\t-c specifies output to console");
  128.     printf("\n\t-mn specifies multiple copies where n is 1..num of copies\n");
  129.     exit();
  130.   }
  131.  
  132.   while (MULTIPLE -- ) {         /* allow for mult copies */
  133.     werr = jzfndfst(mask,32,&wdir);  /* get first matching file */
  134.     if (!werr)
  135.       do {                  /* until no more files     */
  136.     strcpy(wtemp,wpath);
  137.     strcat(wtemp,wdir.dirname);
  138.     if (!( fd = fopen(wtemp,"r"))) {   /* open for read only    */
  139.       /* bad file or it ain't there */
  140.       printf("\nCan't find %s",wdir.dirname);
  141.       exit();
  142.      }
  143.     glbinit();             /* initialize various vars */
  144.     getflags();             /* look at switch settings */
  145.     formfeed ();             /* page feed        */
  146.  
  147.                        /* while not eof do begin */
  148.     while ( (ch = fgets(buf,n,fd)) ) {
  149.       if (linepr(buf)) continue;        /* main print routine      */
  150.       if (! lineslft) formfeed ();        /* formfeed if appropriate */
  151.     }
  152.  
  153.     putlpr('\n');                       /* flush print buffer      */
  154.     fclose(fd);                /* close input file        */
  155.     werr = jzfndnxt(&wdir);         /* get next matching file  */
  156.       } while (! werr);
  157.     else {
  158.       printf("No match for %s",mask);
  159.       exit();
  160.     }
  161.   }
  162. }
  163.  
  164.  
  165. glbinit()                 /* init page and line numbers */
  166. {
  167.   lineno = 0;
  168.   pagenum = 0;
  169. }
  170.  
  171.  
  172. linepr (string)
  173. char *string;
  174. {
  175.   char c;
  176.   int ffflag = 0;
  177.  
  178.   while ( c = *string ++) {
  179.     if ( ( ! colno ) && NUMBER ) {
  180.       printnum(++ lineno);
  181.       colno += 6;
  182.     }
  183.     switch (c) {
  184.  
  185.       case FF :
  186.     ffflag = 1;
  187.     break;
  188.  
  189.       case '\n' :
  190.     putlpr('\n');
  191.     putlpr('\r');
  192.     colno = 0;
  193.     if (! (lineslft --)) formfeed ();
  194.     break;
  195.  
  196.       case '\t' :
  197.     do {
  198.       putlpr (' ');
  199.       colno ++;
  200.       if (! ( ( (NUMBER) ? width - 8 : width) - colno ) ) {
  201.         putlpr('\n');
  202.         putlpr('\r');
  203.         if (! (lineslft --)) formfeed ();
  204.         colno = 0;
  205.       }
  206.     } while ( colno % 8 );
  207.     break;
  208.  
  209.       default:
  210.     putlpr (c);
  211.     colno ++;
  212.     if (! ( ( (NUMBER) ? width - 8 : width) - colno ) ) {
  213.       putlpr('\n');
  214.       putlpr('\r');
  215.       if (! (lineslft --)) formfeed ();
  216.       colno = 0;
  217.     }
  218.     } /* switch */
  219.   }
  220.   if (ffflag) formfeed ();
  221.   return ffflag;
  222. }
  223.  
  224.  
  225. putlpr (c)
  226. char c;
  227. {
  228.   if (CONSOLE) printf("%c",c);
  229.   else {
  230.     inregs.h.ah = 0;
  231.     inregs.h.al = c;
  232.     int86(PROUT,&inregs,&outregs);
  233.   }
  234. }
  235.  
  236.  
  237. formfeed ()
  238. {
  239.   if (FF) putlpr(FF);
  240.   else while (lineslft--) putlpr('\n');
  241.   if (TITLE) heading();
  242.   lineslft = PGLEN - 4;
  243.   colno = 0;
  244. }
  245.  
  246.  
  247. static int EMPHON [] = { '\n',ESC,'E',ESC,'G' };
  248. static int EMPHOFF[] = { '\n','\n','\n','\r',ESC,'F',ESC,'H' };
  249.  
  250. heading()
  251. {
  252.   char strnum[5];
  253.   int i;
  254.   itoa(++pagenum,strnum,10);
  255.   putbuflpr(EMPHON,5);
  256.   strputlpr(TITLE1);
  257.   strputlpr(wdir.dirname);
  258.   strputlpr(TITLE2);
  259.   strputlpr(wasctime);
  260.   strputlpr(TITLE3);
  261.   strputlpr(strnum);
  262.   putbuflpr(EMPHOFF,8);
  263.   getflags();
  264.  
  265. }
  266.  
  267.  
  268. getflags()
  269. {
  270.  
  271.   if (WIDE) {
  272.     putlpr(COMPRS);
  273.     width = 132;
  274.   }
  275.  
  276.   if (DOUBLE) {
  277.     putlpr(ESC);
  278.     putlpr(DBL);
  279.   }
  280.  
  281.   if (EMPHASIZ) {
  282.     putlpr(ESC);
  283.     putlpr(EMPH);
  284.   }
  285.  
  286. }
  287.  
  288.  
  289. putbuflpr(buf,len)
  290. int *buf,len;
  291. {
  292.   int i;
  293.   for ( i = 0 ; i < len ; i ++ ) putlpr(buf[i]);
  294. }
  295.  
  296.  
  297. strputlpr(s)
  298. char *s;
  299. {
  300.   while (*s) putlpr(*s++);
  301. }
  302.  
  303.  
  304. printnum(n)
  305. int n;
  306. {
  307.   int i,j;
  308.   char s[8];
  309.   i = 0;
  310.   strcpy(s,"       ");
  311.   do {
  312.     s [i++] = n % 10 + '0';
  313.     j = i;
  314.   } while ((n /= 10) > 0);
  315.   s [i] = '\0';
  316.   for ( i = 7 ; i > - 1 ; i --) putlpr (s [i]);
  317.   putlpr('>');
  318.   putlpr(' ');
  319. }
  320.